﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharedKernel.Infrastructure.Caches
{
    /// <summary>
    /// 缓存接口
    /// </summary>
    public interface ICacheService
    {
        /// <summary>
        /// 是否正常打开
        /// </summary>
        /// <returns></returns>
        public virtual bool IsOpen() => false;
        /// <summary>
        /// 变更到另外一个数据库
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        void ChangeDataBase(int db);
        /// <summary>
        /// 获取缓存项
        /// </summary>
        /// <typeparam name="T">缓存项类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <returns>缓存项</returns>
        virtual Task<T?> GetAsync<T>(string key) { throw new Exception("暂无实现"); }
        /// <summary>
        /// 设置缓存项
        /// </summary>
        /// <typeparam name="T">缓存项类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存值</param>
        /// <param name="expiration">过期时间</param>
        /// <returns>是否设置成功</returns>
        virtual Task<bool> SetAsync<T>(string key, T value, TimeSpan? expiration = null) { throw new Exception("暂无实现"); }

        /// <summary>
        /// 删除缓存项
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>是否删除成功</returns>
        virtual Task<bool> RemoveAsync(string key) { throw new Exception("暂无实现"); }

        /// <summary>
        /// 检查缓存项是否存在
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>是否存在</returns>
        virtual Task<bool> ExistsAsync(string key) { throw new Exception("暂无实现"); }

        /// <summary>
        /// 获取缓存项
        /// </summary>
        /// <typeparam name="T">缓存项类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <returns>缓存项</returns>
        T? Get<T>(string key);
        /// <summary>
        /// 设置缓存项
        /// </summary>
        /// <typeparam name="T">缓存项类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存值</param>
        /// <param name="expiration">过期时间</param>
        /// <returns>是否设置成功</returns>
        bool Set<T>(string key, T value, TimeSpan? expiration = null);

        /// <summary>
        /// 删除缓存项
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>是否删除成功</returns>
        bool Remove(string key);

        /// <summary>
        /// 检查缓存项是否存在
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>是否存在</returns>
        bool Exists(string key);
    }
}
